﻿package com.example.news;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private String baseUrl="http://v.juhe.cn/toutiao/index?";
    private RecyclerView recyclerView;
    private MyAdapter myAdapter;
    private NewsData newsData;
    private TextView tvTop,tvYule,tvTiyu,tvKeji,tvCaijing;
    private Handler handler;
    private final static int MSG_OK=1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initRecyclerView();
        getDataFromApi("top");
        initHandler();
    }

    private void initHandler() {
        handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch(msg.what) {
                    case MSG_OK:
                        newsData= (NewsData) msg.obj;
                        myAdapter=new MyAdapter(MainActivity.this,newsData);
                        recyclerView.setAdapter(myAdapter);
                        break;
                }
                return false;
            }
        });
    }

    private void initViews() {

        tvTop=findViewById(R.id.tv_top);
        tvYule=findViewById(R.id.tv_yule);
        tvTiyu=findViewById(R.id.tv_tiyu);
        tvKeji=findViewById(R.id.tv_keji);
        tvCaijing=findViewById(R.id.tv_caijing);
        tvTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDataFromApi("top");
            }
        });
        tvYule.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDataFromApi("yule");
            }
        });
        tvTiyu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDataFromApi("tiyu");
            }
        });
        tvKeji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDataFromApi("keji");
            }
        });
        tvCaijing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDataFromApi("caijing");
            }
        });
    }


    private void initRecyclerView() {
        recyclerView=findViewById(R.id.list_View);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }

    private void getDataFromApi(String type){
        OkHttpClient client=new OkHttpClient();
        RequestBody requestBody=new FormBody.Builder()
                .add("type",type)
                .add("key","6ba530bcea7f80a28980d1632179d568")
                .build();
        Request request=new Request.Builder()
                .url(baseUrl)
                .post(requestBody)
                .build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("MainActivity","请求失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String newDataJsonStr=response.body().string();
                newsData=new Gson().fromJson(newDataJsonStr,NewsData.class);
                Log.e("getDataFromApi","newData===="+newsData);
                Message msg=new Message();
                msg.obj=newsData;
                msg.what=MSG_OK;
                handler.sendMessage(msg);
            }
        });
    }
}
